package com.th.supcom.pds.impl.service.internal.phamapply.command;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.InpPhamApplyMaster;
import com.th.supcom.pds.domain.pojo.InpPhamApplyPatInfo;
import com.th.supcom.pds.domain.vo.InpPhamDispenseDetailVO;
import com.th.supcom.pds.impl.service.internal.phamapply.PhamApplyConvertUtils;

/**
 * 获取患者信息
 * 
 * @author leaqua@qq.com
 */
public class ObtainPatInfo extends AbstractPhamApplyManagerBean implements Command
{

    @Override
    public boolean execute (Context arg0) throws Exception
    {
        PhamDispenseContext context = (PhamDispenseContext) arg0;
        InpPhamApplyMaster applyMaster = context.getInpPhamApplyMaster ();
        Map <Long, List <InpPhamDispenseDetailVO>> dispenseMap = context.getDispenseDetailByPaiVisitId ();
        // 通过护理单元查找患者基本信息、费别和可用预交金
        List <InpPhamApplyPatInfo> currentPatInfos = this.getAdapterPTSPatientInfoService ().obtainPatBasicInfo (applyMaster.getNurseUnit ());
        // 获取预交金
        currentPatInfos = this.getAdapterBMSChargeService ().obtainPatAvailablePrepayment (currentPatInfos);
        Map <Long, InpPhamApplyPatInfo> patMap;
        patMap = PhamApplyConvertUtils.convertPatInfoListToMapByPaivisitId (currentPatInfos);
        Set <Map.Entry <Long, InpPhamApplyPatInfo>> set = patMap.entrySet ();
        for (Iterator <Map.Entry <Long, InpPhamApplyPatInfo>> it = set.iterator (); it.hasNext ();)
        {
            Map.Entry <Long, InpPhamApplyPatInfo> entry = (Map.Entry <Long, InpPhamApplyPatInfo>) it.next ();
            InpPhamApplyPatInfo patInfo = entry.getValue ();
            List <InpPhamDispenseDetailVO> temp = dispenseMap.get (entry.getKey ());
            if (CollectionUtils.isEmpty (temp))
            {
                continue;
            }
            for (InpPhamDispenseDetailVO inpPhamDispenseDetailVO : temp)
            {
                inpPhamDispenseDetailVO.setPatName (patInfo.getPatName ());
                inpPhamDispenseDetailVO.setPatientId (patInfo.getPatientId ());
                inpPhamDispenseDetailVO.setVisitCount (patInfo.getVisitId ());
                inpPhamDispenseDetailVO.setChargeType (patInfo.getChargeType ());
            }
        }
        context.setPatMap (patMap);
        return false;
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (ObtainPatInfo.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
